package com.weibo.service.impl;

import com.weibo.dao.BlogDao;
import com.weibo.dao.BlogImgDao;
import com.weibo.dao.CommentDao;
import com.weibo.dao.UserDao;
import com.weibo.entity.Blog;
import com.weibo.entity.BlogImg;
import com.weibo.entity.Comment;
import com.weibo.entity.User;
import com.weibo.resp.BlogDetailAndCommentsResp;
import com.weibo.resp.BlogListItemResp;
import com.weibo.service.BlogService;
import com.weibo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class BlogServiceImpl implements BlogService {

    @Autowired
    BlogDao blogDao;

    @Autowired
    CommentDao commentDao;

    @Autowired
    UserService userService;

    @Autowired
    BlogImgDao blogImgDao;

    @Override
    public List<BlogListItemResp> getList(HashMap<String, Object> param) {
        List<Blog> blogs = blogDao.getList(param);
        List<BlogListItemResp> blogListItemResps = new ArrayList<>();
        blogs.forEach(blog -> {
            BlogListItemResp blogListItemResp = new BlogListItemResp();
            blogListItemResp.setBlogId(blog.getBlogId());
            blogListItemResp.setContent(blog.getContent());
            blogListItemResp.setUser(userService.getUserByUserId(blog.getUserId()));
            blogListItemResp.setCreatedTime(blog.getCreatedTime());
            // 博客的图片
            List<BlogImg> blogImgs = this.getImgs(blog.getBlogId());
//            List<String> imgs = new ArrayList<>();
//            blogImgs.forEach(item ->{
//                imgs.add(item.getUrl());
//            });
//            imgs.add("3.jpg");
//            imgs.add("5.jpg");
            blogListItemResp.setImgs(blogImgs);
            blogListItemResps.add(blogListItemResp);
        });
        return blogListItemResps;
    }

    @Override
    public BlogDetailAndCommentsResp getOne(int blogId) {
        BlogDetailAndCommentsResp resp = new BlogDetailAndCommentsResp();
        Blog blog = blogDao.getOne(blogId);
        List<Comment> comments = commentDao.getList(blogId);
        resp.setBlogId(blogId);
        resp.setContent(blog.getContent());
        resp.setUser(userService.getUserByUserId(blog.getUserId()));
        resp.setCreatedTime(blog.getCreatedTime());
        // 博客的图片
        List<BlogImg> blogImgs = this.getImgs(blog.getBlogId());
//        List<String> imgs = new ArrayList<>();
//        blogImgs.forEach(item ->{
//            imgs.add(item.getUrl());
//        });
        resp.setImgs(blogImgs);
        List<BlogDetailAndCommentsResp.CommentResp> commentList = new ArrayList<>();
        comments.forEach(comment ->{
            BlogDetailAndCommentsResp.CommentResp commentResp = resp.new CommentResp();
            commentResp.setCommentId(comment.getCommentId());
            commentResp.setContent(comment.getContent());
            commentResp.setCreatedTime(comment.getCreatedTime());
            commentResp.setUser(userService.getUserByUserId(comment.getUserId()));

            commentList.add(commentResp);
        });
        resp.setCommentList(commentList);
        return resp;
    }

    @Override
    public void add(Blog blog) {
        blogDao.add(blog);
    }

    @Override
    public void update(Blog blog) {
        blogDao.update(blog);
    }

    @Override
    public void addImg(BlogImg blogImg) {
        blogImgDao.add(blogImg);
    }

    @Override
    public void deleteImg(int imgId) {
        blogImgDao.delete(imgId);
    }

    @Override
    public void addComment(Comment comment) {
        commentDao.add(comment);
    }

    @Override
    public List<BlogImg> getImgs(int blogId) {
        return blogImgDao.getImgs(blogId);
    }
}
